#include "system.h"


//RTthread的任务优先级是越小越高，main函数的优先级是10

//线程的时间片参数只对同等优先级的有效

// 任务优先级
#define START_TASK_PRIO    10   //创建任务的优先级写为10，等待main函数跑完之后，创建任务函数开始运行
// 任务堆栈大小
#define START_STK_SIZE     1024
// 线程控制块指针
static rt_thread_t StartTask_Handler;
// 任务函数声明

//软件定时器
//static rt_timer_t encoder_timer;

static void start_task(void *parameter);

//rt_err_t pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg)
//{
//    rt_err_t result = RT_EOK;
//
//    if (!device)
//    {
//        return -RT_EIO;
//    }
//
//    result = rt_device_control(&device->parent, PWM_CMD_GET, cfg);
//
//    return result;
//}

int main(void)
{
    HardwareInit(); // 硬件初始化
    // 创建启动任务
    StartTask_Handler = rt_thread_create("start_task",
                                        start_task,
                                        RT_NULL,
                                        START_STK_SIZE,
                                        START_TASK_PRIO,
                                        10); // 时间片为100，一个tick为1ms，则为100ms
    if (StartTask_Handler != RT_NULL)
    {
        rt_kprintf("Create initTask succeed\r\n");
        rt_thread_startup(StartTask_Handler); // 启动线程，这里面有触发调度器rt_schedule()，所以会触发调度。
    }
    else
    {
        rt_kprintf("Create initTask fail\r\n");
    }

//    Drive_motor(20000, 20000, -20000, -20000);

//    struct rt_device_pwm *pwm_dev;
//    struct rt_pwm_configuration cfg;
//    // 查找并转换 PWM 设备
//    pwm_dev = (struct rt_device_pwm *)rt_device_find("pwm5");
//    if (pwm_dev == RT_NULL)
//    {
//        rt_kprintf("PWM device not found!\n");
//        return;
//    }
//
//    cfg.channel = 3;
//
//    // 调用 rt_pwm_get 获取当前配置
//    if (pwm_get(pwm_dev, &cfg) == RT_EOK)
//    {
//        rt_kprintf("PWM channel %d: period = %d ns, pulse = %d ns\n",
//                   cfg.channel, cfg.period, cfg.pulse);
//    }
//    else
//    {
//        rt_kprintf("Failed to get PWM configuration.\n");
//    }
    return 0;
}



/* 调度器钩子函数：在每次线程切换时被调用 */
//static void hook_of_scheduler(struct rt_thread* from, struct rt_thread* to)
//{
//#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1))
//    rt_kprintf("[Tick %u] >> switch from: %s --> to: %s\n", rt_tick_get(), from->parent.name, to->parent.name);
//#else
//    rt_kprintf("[Tick %u] >> switch from: %s --> to: %s\n", rt_tick_get(), from->name, to->name);
//#endif
//}

void start_task(void *parameter)
{
    rt_thread_mdelay(1500);
//    rt_scheduler_sethook(hook_of_scheduler);  // 注册钩子

    //陀螺仪线程
//    rt_thread_t Gyroscope_thread = rt_thread_create("Gyroscope_task", Gyroscope_task, RT_NULL, Gyroscope_STK_SIZE, Gyroscope_TASK_PRIO, 0);//陀螺仪任务，该任务的优先级为12
//    if (Gyroscope_thread != RT_NULL) {
//        rt_thread_startup(Gyroscope_thread); //启动线程
//        rt_kprintf("Create Balance_task succeed\r\n");
//    }

//    PID计算任务线程
    rt_thread_t Pid_thread = rt_thread_create("Pid_task", Pid_task, RT_NULL, Pid_task_STK_SIZE, Pid_TASK_PRIO, 0);//PID计算任务，该任务的优先级为11
    if (Pid_thread != RT_NULL) {
        rt_kprintf("Create Pid_thread succeed\r\n");
        rt_thread_startup(Pid_thread); //启动线程
    }

    //摄像头处理数据线程
    rt_thread_t k230_thread = rt_thread_create("k230_rx", k230_data_task, RT_NULL,1024, 14, 0);//该任务的优先级为14
        if (k230_thread != RT_NULL){
            rt_kprintf("Create k230_thread succeed\r\n");
            rt_thread_startup(k230_thread);
    }

//    主任务线程
    rt_thread_t balance_thread = rt_thread_create("Balance_task",Balance_task,RT_NULL,BALANCE_STK_SIZE,BALANCE_TASK_PRIO,0);//车体主控任务，该任务的优先级为13
    if (balance_thread != RT_NULL) {
        rt_kprintf("Create Balance_task succeed\r\n");
        rt_thread_startup(balance_thread); //启动线程
    }


    return ;//默认 return 即可自动退出
}

// 启动任务函数
//void start_task(void *parameter)
//{
////    rt_base_t level;
////    level = rt_hw_interrupt_disable(); // 进入临界区（关中断）
//
//    rt_enter_critical();// 创建线程保护
//
////     创建各功能线程
////    rt_thread_t balance_thread = rt_thread_create("Balance_task",Balance_task,RT_NULL,BALANCE_STK_SIZE,BALANCE_TASK_PRIO,0);//车体主控任务，该任务的优先级为13
//
//    rt_thread_t Gyroscope_thread = rt_thread_create("Gyroscope_task", Gyroscope_task, RT_NULL, Gyroscope_STK_SIZE, Gyroscope_TASK_PRIO, 0);//陀螺仪任务，该任务的优先级为13
//
////    rt_thread_t data_thread = rt_thread_create("DATA_task", data_task, RT_NULL, DATA_STK_SIZE, DATA_TASK_PRIO, 0);//数据处理任务，该任务的优先级为13
//
////    rt_thread_t Pid_thread = rt_thread_create("Pid_task", Pid_task, RT_NULL, Pid_task_STK_SIZE, Pid_TASK_PRIO, 0);//PID计算任务，该任务的优先级为12
//
////    rt_timer_t encoder_timer =  rt_timer_create("enc_timer", encoder_calc_entry,RT_NULL, ENCODER_CALC_PERIOD_MS, RT_TIMER_FLAG_PERIODIC);//编码器软件定时器，它的线程优先级为4
//
//
//
////    if (balance_thread != RT_NULL) {
////        rt_kprintf("Create Balance_task succeed\r\n");
////        rt_thread_startup(balance_thread); //启动线程
////    }
//    if (Gyroscope_thread != RT_NULL) {
//        rt_kprintf("Create Balance_task succeed\r\n");
//        rt_thread_startup(Gyroscope_thread); //启动线程
//    }
////    if (Pid_thread != RT_NULL) {
////        rt_kprintf("Create Pid_thread succeed\r\n");
////        rt_thread_startup(Pid_thread); //启动线程
////    }
////    if (encoder_timer != RT_NULL) {
////        rt_kprintf("Create encoder_timer succeed\r\n");
////        rt_timer_start(encoder_timer); //启动软件定时器
////    }
//
////    if (data_thread != RT_NULL) {
////        rt_kprintf("Create Balance_task fail\r\n");
////        rt_thread_startup(data_thread);
////    }
//
////    rt_hw_interrupt_enable(level); // 退出临界区（开中断），只是允许中断可以来了，但不会发生调度，只有当前线程主动挂起或删除自己，或者有中断时才会有触发调度
//    rt_exit_critical();//退出线程保护
//
////     删除启动任务自身
////    rt_thread_delete(rt_thread_self());
//}

//rt_thread_dump();  // 打印当前所有线程状态
/* 导出到 msh 命令列表中 */
//MSH_CMD_EXPORT(pulse_encoder_sample, pulse encoder sample);
